<template>
  <div class="login-container">
    <el-alert v-show="false" title="后台管理" type="success" :closable="false" style="position: fixed"></el-alert>
    <el-row>
      <el-col :xs="24" :sm="24" :md="12" :lg="14" :xl="14">
        <div style="color: transparent">左侧区域占位符</div>
      </el-col>
      <el-col :xs="24" :sm="24" :md="12" :lg="10" :xl="10">
        <el-form status-icon class="login-form">
          <div class="form-header">
            <div class="title">hello !</div>
            <div class="title-tips">
              欢迎来到后台管理！
            </div>
          </div>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" autocomplete="off" v-model="userForm.username" placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" :prefix-icon="Lock" v-model="userForm.password" autocomplete="off" placeholder="请输入密码" />
          </el-form-item>
          <el-form-item prop="captchaCode">
            <el-row>
              <el-col :span="15">
                <el-input autocomplete="off" v-model="userForm.captchaCode"  placeholder="请输入验证码" />
              </el-col>
              <el-col :span="8" :offset="1">
                <el-image fit="contain" :src="imageUrl"  @click="getImageUrl()" 
                style="height: 100%; background: white" class="pointer" />
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item>
            <el-button class="login-btn" @click="login()" type="primary" size="large">
              登陆
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">

import { User, Lock } from '@element-plus/icons-vue'

import {getCaptchaCode} from '@/api/user/index';
import {ref,onMounted} from 'vue';

//1. 实现验证码显示的功能
//1.1 声明接收图片地址的响应式字符串 
let imageUrl = ref("");


//1.2 定义图片获取的异步方法
let getImageUrl = async()=>{
  let {data:{image,key}} = await getCaptchaCode();
  imageUrl.value = image;
  //给key赋值
  userForm.value.captchaKey = key;
}

//1.3 周期调用获取验证码的方法
onMounted(()=>{
  getImageUrl();
});



//2.触发登录调用功能 
import userPinia from '@/store/modules/user/userStore';
import {useRouter} from 'vue-router';

// 导入依赖 pinia | useRouter
// 声明传递的参数对象
let userForm = ref({
    username:"",
    password:"",
    captchaKey:"",
    captchaCode:""
})
// 声明参数对象要表单双向绑定
// 给登录按钮添加点击事件 -> pinia -> doLogin -> push("/")
let userStore = userPinia();
let router = useRouter();

let login =async ()=>{
  //await确保登录成功才会跳转
  await userStore.doLogin(userForm.value);
  //一定成功
  console.log(userStore.token);
  router.push("/");
}


</script>

<style scoped lang="scss">
@import './index';
</style>
